COM Commands for SMS
COM
COM Used variables from ini file:
COM PrimaryMobilePhoneNumber: own mobile phone number MSISDN
COM SecondaryMobilePhoneNumber: 2nd mobile phone number as SMS receiver
COM
COM Version History:
COM Version 1.0   19.11.2003   R. Weiden	started...
COM Version 1.1   12.04.2004   R. Weiden	added AT+CSCA, AT+CSCB
COM Version 2.0   24.08.2004   R. Weiden	adaptation to ATT2.0, with SMS_PDU_processing.py 
COM Version 2.1   25.08.2004   R. Weiden	several improvements, eg. testing of unsolicited result code +CMTI


COM <font color="#3333FF"><strong>THIS SCRIPT REQUIRES TWO MOBILES CONNECTED TO LOGICAL PORT 1 AND 2!</strong></font>
# ---------------------------------------------------------------------------------

# prepare for PDU Processing

import SMS_PDU_processing
from attglobals import *

PduProcessor=SMS_PDU_processing.CPdu()

# ---------------------------------------------------------------------------------
# Variable assignments: Enter own messages here for sending via SMS, if required...

TEXT1 = 'Hello! This is ATC test message 1 from ATT2.0'
TEXT2 = 'Hello! This is ATC test message 2 from ATT2.0'

# set extra long timeout time for SMS receiving (in ms)
TIMEOUT_LONG=25000

# create corresponding PDUs for later use
strPDU_TEXT1=PduProcessor.ConvertASCII2PDU(TEXT1)
strPDU_TEXT2=PduProcessor.ConvertASCII2PDU(TEXT2)
 
# ---------------------------------------------------------------------------------
COM check availability of used commands

AT+CMGW=?
WAITFOR OK
AT+CMGS=?
WAITFOR OK
AT+CMGD=?
WAITFOR OK
AT+CPMS=?
WAITFOR OK
AT+CMGL=?
WAITFOR OK
AT+CMSS=?
WAITFOR OK
AT+CMGF=?
WAITFOR OK
AT+CNMI=?
WAITFOR OK

COM activate extended error messages
AT+CMEE=2
WAITFOR OK

COM select preferred storage
AT+CPMS="MT","MT","MT"
WAITFOR OK
AT+CPMS?
WAITFOR +CPMS: "MT",

# -------------------------------------------------------------------------------------------------
COM AT+CSCA Selection of message service

AT+CSCA=?
WAITFOR OK

COM Get Service Center Address from mobile 
AT+CSCA?
strCSCAResult=WAITFOR (1, '+CSCA:')

strSCA=ExtractParameter (strCSCAResult, 1, '+CSCA: "', '",\n')
iSCAFormat=int (ExtractParameter (strCSCAResult, 2, '+CSCA: "', '",\n'))

COM ('Currently set in mobile: SCA: ', strSCA, ', Format: ', iSCAFormat)

COM Set service center address to dummy value
AT+CSCA='+49234599999'
WAITFOR OK
COM Verify
AT+CSCA?
WAITFOR +CSCA: "+49234599999"
COM Reset to old value
AT+CSCA=strSCA
WAITFOR OK
COM Verify
AT+CSCA?
WAITFOR (1, '+CSCA: "', strSCA, '"')

# -------------------------------------------------------------------------------------------------
COM AT+CSCB Cell broadcast

AT+CSCB=?
WAITFOR +CSCB:
COM switch of cell broadcast
AT+CSCB=1
WAITFOR OK
COM verify
AT+CSCB?
WAITFOR +CSCB: 1,
COM reset
AT+CSCB=0
WAITFOR OK
COM verify
AT+CSCB?
WAITFOR +CSCB: 0,
COM try nonsense
AT+CSCB=2
WAITFOR ERROR

# -------------------------------------------------------------------------------------------------
COM SMS functionality tests: writing, sending and reading messages

# check AT+CMGW

COM ('Write message "', TEXT1, '" for receiver ', PrimaryMobilePhoneNumber, ' (primary mobile phone) to SMS storage.')

# create PDU from TEXT1
PduProcessor.m_strServiceCenterAddress=strSCA
PduProcessor.m_strDestinationAddress=PrimaryMobilePhoneNumber
PduProcessor.m_strUserData=TEXT1
iPduLen=PduProcessor.CreateSubmitPDU()
strSubmitPDU_TEXT1=PduProcessor.m_strCurrentPDU

COM ('Message text in PDU format: ', PduProcessor.m_strCurrentPDU, ' length: ', iPduLen)
 
AT+CMGW=iPduLen
WAITFOR >
COM now write PDU to mobile, including trailing CTRL-Z (ASCI 26)
ATCMD (1, PduProcessor.m_strCurrentPDU, chr(26))
strCMGWResult=WAITFOR (1, 'OK')

# Get index of storage position of previously stored SMS 
iIndex=int (ExtractParameter(strCMGWResult, 1, '+CMGW:', '\n'))

# -------------------------------------------------------------------------------------------------
# check AT+CMGR

COM ('Last message was written to index ', iIndex, '.\nTest reading back written message from sms storage')
AT+CMGR=iIndex
WAITFOR (1, PduProcessor.m_strCurrentPDU)

# -------------------------------------------------------------------------------------------------
# check AT+CNMI

COM switch on unsolicited result code +CMTI: for message receiving
AT+CNMI=1,1
WAITFOR OK
COM verify
AT+CNMI?
WAITFOR +CNMI: 1,1,

# -------------------------------------------------------------------------------------------------
# check AT+CMGS

COM ('Send SMS MESSAGE "', TEXT2, '" to own number ', PrimaryMobilePhoneNumber, ' (primary mobile phone)')

# create PDU from TEXT2
PduProcessor.m_strServiceCenterAddress=strSCA
PduProcessor.m_strDestinationAddress=PrimaryMobilePhoneNumber
PduProcessor.m_strUserData=TEXT2
iPduLen=PduProcessor.CreateSubmitPDU()

COM ('Message text in PDU format: ', PduProcessor.m_strCurrentPDU, ' length: ', iPduLen)

AT+CMGS=iPduLen
# WAIT FOR "AT+CMGS= <PDU_LENGTH>LFLFCR>" (that's very exact! :-)) 
WAITFOR (1, 'AT+CMGS=', iPduLen, chr(13), chr(13), chr(10), '>')

COM now send PDU to mobile, including trailing CTRL-Z (ASCI 26)
ATCMD (1, PduProcessor.m_strCurrentPDU, chr(26))
WAITFOR OK

# -------------------------------------------------------------------------------------------------
COM Wait for incoming message. Should cause unsolicited result code +CMTI: 

# clear buffer from port 1, because this will still contain the result of last AT command
ClearReceiveBuffer(1)

# receiving of SMS may take some time. Wait extra long...
iProjectTimeout=SetReceiveTimeout(TIMEOUT_LONG)

strResultCode=WAITFOR (1, '+CMTI:')

# get index and storage position of received SMS. First check if no timeout occured...
if strResultCode is not None:
	iReceivedMessageStorage=ExtractParameter(strResultCode, 1, '+CMTI: ', ',\n')
	iReceivedMessageIndex=int (ExtractParameter(strResultCode, 2, '+CMTI: ', ',\n'))
else:
	iReceivedMessageStorage=""
	iReceivedMessageIndex=0
	COM <font color="#FF3333"><strong> A TIMEOUT OCCURED WHILE WAITING FOR INCOMING MESSAGE!</strong></font>

SetReceiveTimeout(iProjectTimeout)

# -------------------------------------------------------------------------------------------------
# check AT+CMGR again

COM ('Read back received message 2 from index ', iReceivedMessageIndex, ' in storage ', iReceivedMessageStorage)

AT+CMGR=iReceivedMessageIndex
WAITFOR (1, strPDU_TEXT2)  

COM List received messages
COM List message in UNSENT folder (should contain message #1)
AT+CMGL=2
WAITFOR (1, strSubmitPDU_TEXT1)		# should be identical to complete SUBMIT PDU (unchanged because not yet sent)

COM List message in READ folder (should now contain message #2)
AT+CMGL=1
WAITFOR (1, strPDU_TEXT2)

COM Delete received message in READ folder
AT+CMGD=iReceivedMessageIndex
WAITFOR OK

COM check if message is really deleted
AT+CMGR=iReceivedMessageIndex
WAITFOR +CMGR: 0,,0

# -------------------------------------------------------------------------------------------------
COM Send message from UNSENT folder (message #1)
AT+CMSS=iIndex
WAITFOR +CMSS:

COM This message should now be moved to SENT folder
AT+CMGL=3
WAITFOR (1, strPDU_TEXT1)

COM Wait for incoming message with TEXT1. Should cause unsolicited result code +CMTI: 
iProjectTimeout=SetReceiveTimeout(TIMEOUT_LONG)

ClearReceiveBuffer(1)
strResultCode=WAITFOR (1, '+CMTI:')

# get index and storage position of received SMS
if strResultCode is not None:
	iReceivedMessageStorage=ExtractParameter(strResultCode, 1, '+CMTI: ', ',\n')
	iReceivedMessageIndex=int (ExtractParameter(strResultCode, 2, '+CMTI: ', ',\n'))
else:
	iReceivedMessageStorage=""
	iReceivedMessageIndex=0	
	COM <font color="#FF3333"><strong> A TIMEOUT OCCURED WHILE WAITING FOR INCOMING MESSAGE!</strong></font>

SetReceiveTimeout(iProjectTimeout)

COM Read back received message 1. Use DEFAULT for CMGL: 0
AT+CMGL
WAITFOR (1, strPDU_TEXT1) 

COM Delete received message in READ folder
AT+CMGD=iReceivedMessageIndex
WAITFOR OK

COM check if message is really deleted
AT+CMGR=iReceivedMessageIndex
WAITFOR +CMGR: 0,,0

# -------------------------------------------------------------------------------------------------
COM ('Send message #1 to secondary phone ', SecondaryMobilePhoneNumber) 

iProjectTimeout=SetReceiveTimeout(1200)

COM First switch on unsolicited result code +CMTI: for mobile 2 and check if mobile answers
2:AT+CNMI=1,1
strResultCode=WAITFOR (2, 'OK')

if strResultCode is None or 'OK' not in strResultCode:
	COM <font color="#3333FF">THERE SEEMS TO BE NO MOBILE ON PORT 2! TESTCASE SKIPPED!</font>
else:
	COM Set preferred storage for mobile 2
	2:AT+CPMS="MT","MT","MT"
	2:WAITFOR OK

	SetReceiveTimeout(TIMEOUT_LONG)

	COM ('send message #1 to phone 2. Was written to Index ', iIndex, ' above')
	1:AT+CMSS=iIndex, SecondaryMobilePhoneNumber, 145
	1:WAITFOR +CMSS:
	
	COM Wait for incoming message #1 on mobile 2. Should cause unsolicited result code +CMTI: 
	ClearReceiveBuffer(2)
	strResultCode=WAITFOR (2, '+CMTI:')

	# if message correctly received, get storage and index
	if strResultCode is not None:
		# get index and storage position of received SMS
		iReceivedMessageStorage=ExtractParameter(strResultCode, 1, '+CMTI: ', ',\n')
		iReceivedMessageIndex=int (ExtractParameter(strResultCode, 2, '+CMTI: ', ',\n'))

		COM ('Phone 2: Read back received message 2 from index ', iReceivedMessageIndex, ' in storage ', iReceivedMessageStorage)
		2:AT+CMGR=iReceivedMessageIndex
		WAITFOR (2, strPDU_TEXT1)  
		
		COM delete received message
		2:AT+CMGD=iReceivedMessageIndex
		2:WAITFOR OK

	else:
		COM <font color="#FF3333"><strong>A TIMEOUT OCCURED WHILE WAITING FOR INCOMING MESSAGE!</strong></font>

	# reset unsolicited result code for mobile 2
	2:AT+CNMI=0,0
	2:WAITFOR OK

SetReceiveTimeout(iProjectTimeout)

COM delete message 1 in mobile 1
AT+CMGD=iIndex
WAITFOR OK

COM check if message is really deleted
AT+CMGR=iIndex
WAITFOR +CMGR: 0,,0


# -------------------------------------------------------------------------------------------------
COM Consistency check of SMS related commands as far as not already done above

COM Check AT+CMGD command, try to delete errorly index 
AT+CMGD=0
WAITFOR ERROR

COM Check AT+CMGF command
AT+CMGF=0
WAITFOR OK
AT+CMGF?
WAITFOR +CMGF: 0
COM set nonsense value
AT+CMGF=1
WAITFOR ERROR

COM Check AT+CMGL command
AT+CMGL=5
WAITFOR +CMS ERROR
AT+CMGL?
WAITFOR +CMS ERROR

COM Check AT+CMGR command
COM read empty index #9 (was deleted on script startup)
AT+CMGR=9
WAITFOR +CMGR: 0,,0
COM Check with errorly commands
AT+CMGR?
WAITFOR +CMS ERROR
AT+CMGR=300
WAITFOR +CMS ERROR

COM Check AT+CMGS command
COM try sending PDU with incorrect length
AT+CMGS=35
WAITFOR >
ATCMD (1, '079194712272303301000C9194712295541000001A54747A0E4ACF41EDF27C1E3E9741329059FE6E8382542A', chr(26))
WAITFOR +CMS ERROR
COM Check nonsense read command
AT+CMGS?
WAITFOR ERROR
COM try sending incorrect PDU
AT+CMGS=36
WAITFOR >
ATCMD (1, '079194712272303301000C9194712295541000001C54747A0E4ACF41EDF27C1E3E9741329059FE6E8382542A', chr(26)) 
WAITFOR +CMS ERROR

COM Check AT+CMGW command
COM try writing PDU with incorrect length
AT+CMGW=35
WAITFOR >
ATCMD (1, '079194712272303301000C9194712295541000001A54747A0E4ACF41EDF27C1E3E9741329059FE6E8382542A', chr(26))
WAITFOR +CMS ERROR
COM Check nonsense read command
AT+CMGW?
WAITFOR +CMS ERROR
COM try writing incorrect PDU
AT+CMGW=36
WAITFOR >
ATCMD (1, '079194712272303301000C9194712295541000001C54747A0E4ACF41EDF27C1E3E9741329059FE6E8382542A', chr(26))
WAITFOR +CMS ERROR

COM Check AT+CMSS command
COM try nonsense read command
AT+CMSS?
WAITFOR +CMS ERROR
COM try international number with national destination address format
AT+CMSS= 1, PrimaryMobilePhoneNumber, 129
WAITFOR ERROR

# ---------------------------------------------------------------------------------

COM reset extended error messages
AT+CMEE=0
WAITFOR OK

COM reset unsolicited result code for mobile 1
AT+CNMI=0,0
WAITFOR OK
